<?php
/**
 * 用户控制器
 * Author: 孙武 QQ:1228746736
 * Date: 2017/11/1
 * Time: 18:50
 */

namespace Home\Controller;

use Home\Model\UserModel;
use Addons\WeChatApi\WeChat;

class UserController extends HomeController
{

    private $_M;

    public function __construct()
    {

        parent::__construct();
        $this->_M = new UserModel('user');

    }

    /**
     * 用户首页
     */
    public function index()
    {
        $this->display();
    }


    /**
     * 订票已付款/预约成功已付款
     */
    public function hadByTicket()
    {
        $this->display();
    }


    /**
     * 未付款
     */
    public function hadNoPayTicket()
    {

    }


    /**
     * 预约中
     */
    public function orderingTicket()
    {

    }

    /**
     * 预约成功未付款
     */
    public function hadOrderNoPayTicket()
    {

    }


    /**
     * 我的成长
     */
    public function growing()
    {
        $hadInvitePerson=$this->_M->getHadInvitePerson();//邀请的人数
        $hadByTicket=$this->_M->getHadBuyTime();//购票次数
        $joinTime=$this->_M->getJoinTime();//加入时间
        $hadJoinTime=ceil((time()-$joinTime)/60/60/24);
        $this->assign("hadInvitePerson",$hadInvitePerson);
        $this->assign("hadByTicket",$hadByTicket);
        $this->assign("joinTime",$joinTime);
        $this->assign("hadJoinTime",$hadJoinTime);
        $this->display();
    }

    /**
     * 邀请有礼
     */
    public function invite()
    {


        $usrId = session('userId');
        $fileName =WEB_ROOT. "/Public/UserQR/" . $usrId . ".jpg";
        //检查二维码是否存在
        if (file_exists($fileName)) {

            if (C('HOME_DEBUG')) {
                echo "QR文件存在";
            }
            $this->assign("QRUrl",C('QR_IMG').$usrId.".jpg");

        } else {
            //todo 生成分享二维码
            $weChat=new WeChat(C('APP_ID'),C('APP_SECRET'));
            $createQRTime="./Public/UserQR/" . session('userId');
            $creTool=$weChat->getQR(2, session('userId'),$createQRTime);
            if (C('HOME_DEBUG')) {
                echo "文件不存在<br>";
                echo "生成QR的文件名:".$createQRTime."<br>";
                echo "$creTool"."<br>";

            }
            $this->assign("QRUrl",C('QR_IMG').$usrId.".jpg");
        }
        $this->display('invite');
    }

    /**
     * 邀请有礼的规则
     */
    public function invite_rule()
    {
        $this->display();
    }


    /**
     * 个人信息
     */
    public function userInfo()
    {
        $userId = session('userId');
        $userInfo = $this->_M->getUserInfoById($userId);
        $userInfo['usr_birthday']=date ( "Y年m月d日",$userInfo['usr_birthday']);
        $this->assign("userInfo",$userInfo);
        $this->display();
    }


    /**
     * 修改个人信息
     */
    public function modifyUserInfo()
    {
        if (IS_POST) {
            $data = $this->_M->create();

            //strtotime("2015-05-22 15:00:00")
            /*处理时间信息*/

            $timeStr=$data['usr_birthday']." "."00:00:00";
            $data['usr_birthday']=strtotime($timeStr);
            if(($res=$this->checkModifyInfoIsLegal($data))!==true){
                if (C('HOME_DEBUG')) {
                    dump($res);
                }
                $this->err($res);exit;
            }

            $id = session('userId');
            if ($this->_M->where("usr_id =$id")->save($data)) {
                $this->ok('修改成功');
                $this->redirect('userInfo');
            } else {
                $this->err('服务器繁忙,请稍后再试');
            }

        } else {

            $userInfo=$this->_M->getUserInfoById(session('userId'));
            $userInfo['usr_birthday']=date('Y-m-d',$userInfo['usr_birthday']);
            $this->assign("u",$userInfo);
            $this->display();
        }
    }

    /**
     * 反馈
     */
    public function feedbak()
    {
        if (IS_POST) {
            $dbData = array(
                'fek_msg' => $_GET['suggest'],
                'fek_tel' => $_GET['tel'],
                'usr_id' => session('userId')
            );
            if (empty($dbData['fek_msg']) || empty($dbData['fek_msg'])) {
                $this->err("请完善反馈信息");


            } else {
                if (D('feedbak')->add($dbData)) {
                    $this->ok('反馈成功');
                    $this->successRedirect('Home/User/index');
                    exit;
                }
            }


        } else {
            $this->display();
        }

    }


    /**
     * 我的优惠券
     */
    public function saverTicket()
    {
        $usrId=session('userId');
        $sql="select * from bus_discount where user_id=$usrId and dc_type=1";

        $monData=D()->query($sql);
        if (C('HOME_DEBUG')) {
            dump($monData);
        }

        $sql1="select * from bus_discount where user_id=$usrId and dc_type=2";
//        $perData=D('discount')->where(array('user_id'=>$usrId,"dc_type"=>2))->query();
        $perData=D()->query($sql1);
        if (C('HOME_DEBUG')) {
            dump($perData);
        }
        $this->assign("monInfo",$monData);
        $this->assign("perInfo",$perData);
        $this->display();
    }

    private function checkModifyInfoIsLegal($data)
    {
        if(strlen($data['usr_title'])<2){
            return '用户昵称不能少于两个字';
        }
        if(strlen($data['usr_name'])<2){
            return '用户姓名不能少于两个字';
        }
        if(strlen($data['usr_id_card'])<18){
            return '用户身份证不能少于18个数字';
        }
        if(strlen($data['usr_qq'])<8){
            return '用户qq不能少于数字';
        }
        if(strlen($data['usr_campus'])<2){
            return '用户校园不能少于两个字';
        }
        if(strlen($data['usr_tel'])<11){
            return '用户电话格式错误';
        }
        return true;

    }
}